local pastoral_tf_spicedfoods = require("pastoral_tf_spicedfoods") -- 调味料理
local cooking = require("cooking")
local _G = GLOBAL

-- 调味粉相关,注意配置
if not _G.SETS_PASTORAL.ENABLEDMODS["tastefun"] then
    -- 调味后料理额外三维恢复
    AddPrefabPostInitAny(function(inst)
        if not TheWorld.ismastersim then
            return
        end

        if inst:HasTag("spicedfood")
            and inst.components.edible and inst.components.edible.spice and pastoral_tf_spicedfoods.TF_SPICES[inst.components.edible.spice]
            and inst.components.inventoryitem
        then
            local spicename = string.lower(inst.components.edible.spice)
            if spicename == "spice_tf_orange_jam" then
                inst.components.edible.sanityvalue = inst.components.edible.sanityvalue + 20
            elseif spicename == "spice_tf_strawberry_jam" then
                inst.components.edible.healthvalue = inst.components.edible.healthvalue + 20
            end
        end
    end)

    -- 调味后料理替换调味料贴图，但是我们的swapbuild不是spices，必须改成我们自己的动画build
    local OldOverrideSymbol = AnimState.OverrideSymbol
    AnimState.OverrideSymbol = function(self, symbol, swapbuild, swapsymbol, ...)
        if symbol == "swap_garnish" and swapbuild == "spices" and swapsymbol and pastoral_tf_spicedfoods.TF_SPICES[string.upper(swapsymbol)] then
            swapbuild = "tf_spices"
        end
        return OldOverrideSymbol(self, symbol, swapbuild, swapsymbol, ...)
    end

    -- Mod加载完后给所有食谱添加一遍新调料的料理，以下代码参考了风铃大佬的小穹，风铃大佬牛逼！
    local oldRegisterPrefabs = GLOBAL.ModManager.RegisterPrefabs

    GLOBAL.ModManager.RegisterPrefabs = function(self, ...)
        -- 这个时候 PrefabFiles文件还没有被加载

        for k, v in pairs(cooking.recipes) do
            if k and v and k ~= "portablespicer" then
                pastoral_tf_spicedfoods.Tf_GenerateSpicedFoods(v)
            end
        end
        -- 添加新的调味料理食谱
        for k, v in pairs(pastoral_tf_spicedfoods.TfGetNewRecipes()) do
            AddCookerRecipe("portablespicer", v)
        end
        oldRegisterPrefabs(self, ...)
    end
end

-- 兼容棱镜的异种,棱镜相关的代码文件:datafix_legion.lua
if _G.SETS_PASTORAL.ENABLEDMODS["legion"] and (not _G.SETS_PASTORAL.ENABLEDMODS["tastefun"]) then
    -- 棱镜专属的异种种子全局表,根据优先级判断是否创建
    if not _G.rawget(_G, "TRANS_DATA_LEGION") then -- 对于global来说，不能直接检测是否有某个元素，需要用rawget才行
        _G.TRANS_DATA_LEGION = {}
    end

    local mapseeds = {
        tf_orange_oversized = {
            swap = { build = "farm_plant_tf_orange", file = "swap_body", symboltype = "3" },
            fruit = "seeds_tf_orange_l",
        },
    }

    for k, v in pairs(mapseeds) do
        _G.TRANS_DATA_LEGION[k] = v
    end
    mapseeds = nil

    -- 棱镜专属的异种作物的全局表,根据优先级判断是否创建
    if not _G.rawget(_G, "CROPS_DATA_LEGION") then
        _G.CROPS_DATA_LEGION = {}
    end

    local xmult = 1
    local time_crop = 12 * TUNING.TOTAL_DAY_TIME * xmult -- 普通作物一般是5天生长期
    local time_day1 = TUNING.TOTAL_DAY_TIME * xmult
    local time_ripe = time_day1 * 6                      -- 过熟时间默认6天

    -- witheredprefab 代表枯萎时被挖起的产物
    _G.CROPS_DATA_LEGION.tf_orange = {        -- 橘子异种
        growthmults = { 1.8, 1.5, 1.8, 0.6 }, -- 春夏秋冬:大于1为快,小于1为慢
        regrowstage = 2,
        bank = "crop_tf_orange",
        build = "crop_tf_orange",
        leveldata = {
            { anim = "idle_1", time = time_day1,        deadanim = "dead", witheredprefab = nil },
            { anim = "idle_2", time = time_crop * 0.45, deadanim = "dead", witheredprefab = { "log" } },
            { anim = "idle_3", time = time_crop * 0.55, deadanim = "dead", witheredprefab = { "log" }, bloom = true }, -- bloom 表示开花
            {
                anim = {
                    "idle_4_less", "idle_4_normal", "idle_4_more"
                },
                time = time_ripe,
                deadanim = "dead",
                witheredprefab = { "log", "log" }
            }
        },
        cluster_size = { 1, 1.5 } -- 簇栽动画倍率最低为 1 最高为 1.5
    }
end
